
module.exports = function({ types: t }, options) {
    console.log('options', options);
    return {
      pre(file) {
        // console.log('pre-pre');
      },
      visitor: {
        MemberExpression(path, state) {
            // console.log('po------01');
            // console.log(path.node);
            // 查询到指定的节点
            const obj = path.get('object');
            const property = path.get('property');
            if (obj && obj.node.name !== options.obj) {
              return;
            } else if (property && property.node.name !== options.pro) {
              return;
            }
            if (property && property.node.name === options.pro && obj && obj.node.name === options.obj) {
              // console.log('find-node', path.node);
              // 查找指定的父节点
              // 以此为基础不断的向上查找，获取params、success、fail、error、complete
              // console.warn('findParent', p.node);
              // console.warn('findParent-isCallExpression', p.isCallExpression());

              // Http.callService(params)   p
              // 处理参数
              const p = path.findParent((p) => p.isCallExpression());
              // console.warn('findParent', p.node);
              const cacheArg = p.get('arguments.0');
              // console.warn('findParent-arguments', cacheArg && cacheArg.node);

              let successArgNodePath;
              let failArgNodePath;
              let errorArgNodePath;
              let completeArgNodePath;
              let repNodePath;

              let flag = true;
              let pc = p;
              while (flag) {
                const psuc = pc.findParent((p) => p.isMemberExpression());
                if (psuc) {
                  const identifierName = psuc.get('property').node.name;
                  // console.warn('identifierName', identifierName);
                  if (identifierName === 'success') {
                    pc = pc.findParent((p) => p.isCallExpression());
                    successArgNodePath = pc.get('arguments.0');
                  } else if (identifierName === 'fail') {
                    pc = pc.findParent((p) => p.isCallExpression());
                    failArgNodePath = pc.get('arguments.0');
                  } else if (identifierName === 'error') {
                    pc = pc.findParent((p) => p.isCallExpression());
                    errorArgNodePath = pc.get('arguments.0');
                  } else if (identifierName === 'complete') {
                    pc = pc.findParent((p) => p.isCallExpression());
                    completeArgNodePath = pc.get('arguments.0');
                  } else {
                    console.warn('eeeeeeee');
                    break;
                  }
                } else {
                  repNodePath = pc.findParent((p) => p.isExpressionStatement() || p.isReturnStatement());
                  break;
                }
              }

              // console.warn('repNodePath', repNodePath.node);
              // 创建新节点替换
              // 如果传参传递的是参数变量
              const methodKey = t.objectProperty(t.identifier('methods'), t.stringLiteral('post'));
              const keys = [methodKey];
              if (cacheArg) {
                if (cacheArg.isObjectExpression()) {
                  cacheArg.node.properties.forEach((n) => {
                    if (n.key.name === 'url') {
                      keys.push(t.objectProperty(t.identifier('api'), t.cloneDeep(n.value)));
                    } else if (n.key.name === 'data'){
                      keys.push(t.objectProperty(t.identifier('params'), t.cloneDeep(n.value)));
                    }
                  });
                } else if (cacheArg.isIdentifier()) {
                  // 处理参数
                  let hasUrl = false, hasData = false;
                  repNodePath.getAllPrevSiblings().forEach((n) => {
                    if (n.isVariableDeclaration()) {
                      const declarations = n.get('declarations');
                      declarations.forEach((d) => {
                        if (d.get('id').node.name === cacheArg.node.name) {
                          const di = d.get('init');
                          if (di.isObjectExpression()) {
                            di.get('properties').forEach((dip) => {
                              const nn = dip.get('key').node.name;
                              if (nn === 'url') {
                                hasUrl = true;
                              } else if (nn === 'data') {
                                hasData = true;
                              }
                            });
                          }
                        }
                      });
                    }
                  });
                  if (hasUrl) {
                    keys.push(t.objectProperty(t.identifier('api'), t.memberExpression(t.identifier(cacheArg.node.name), t.identifier('url'))));
                  }
                  if (hasData) {
                    keys.push(t.objectProperty(t.identifier('params'), t.memberExpression(t.identifier(cacheArg.node.name), t.identifier('data'))));
                  }
                  // 处理参数
                }
              }
              if (successArgNodePath) {
                keys.push(t.objectProperty(t.identifier('onsuccess'), t.cloneDeep(successArgNodePath.node)));
              }
              if (errorArgNodePath) {
                keys.push(t.objectProperty(t.identifier('onerror'), t.cloneDeep(errorArgNodePath.node)));
              }
              if (failArgNodePath) {
                keys.push(t.objectProperty(t.identifier('onfail'), t.cloneDeep(failArgNodePath.node)));
              }
              if (completeArgNodePath) {
                keys.push(t.objectProperty(t.identifier('oncomplete'), t.cloneDeep(completeArgNodePath.node)));
              }
              const pNode = t.objectExpression(keys);
              if (repNodePath.isExpressionStatement()) {
                const nd = t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('window.niceloo.api'), t.identifier('callRaw')), [pNode]));
                repNodePath.replaceWith(nd);
              } else if (repNodePath.isReturnStatement()) {
                const nd = t.returnStatement(t.callExpression(t.memberExpression(t.identifier('window.niceloo.api'), t.identifier('callRaw')), [pNode]));
                repNodePath.replaceWith(nd);
              }
            }
            
        },
      },
      post(file) {
        // console.log('post-post');
      }
    };
  };